package zym.diandian.tools;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NetworkHelper {

    public static String ip = "UNKNOWN";

    public static final String UNKNOWN = "UNKNOWN";


    public static String[] getNetInfo(Context context) {
        String[] result = new String[7];
        try {
            String yys = DisplayHelper.getProp("gsm.sim.operator.numeric").trim();
            if (!TextUtils.isEmpty(yys)) {
                int code = Integer.parseInt("" + yys);
                result[0] = "" + NetworkHelper.getYYS(code);
            } else {
                if (NetworkHelper.isAirModeOn(context)) {
                    result[0] = "飞行模式";
                } else {
                    result[0] = "未知";
                }
            }
            result[1] = "" + NetworkHelper.ip;
            result[2] = "" + DisplayHelper.getProp("net.dns1");
            result[3] = "" + DisplayHelper.getProp("net.dns2");
            result[4] = "" + DisplayHelper.getProp("net.hostname");
            result[5] = "";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * get networktype ,diff sim type
     *
     * @param context
     * @param code
     * @return
     */
    public static String getNetworkType(Context context, int code) {
        String t = UNKNOWN;
        switch (code) {
            case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                t = UNKNOWN;
                break;
            case TelephonyManager.NETWORK_TYPE_GPRS:
                t = "GPRS";
                break;
            case TelephonyManager.NETWORK_TYPE_EDGE:
                t = "EDGE";
                break;
            case TelephonyManager.NETWORK_TYPE_UMTS:
                t = "UMTS";
                break;
            case TelephonyManager.NETWORK_TYPE_CDMA:
                t = "CDMA";
                break;
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
                t = "EVDO_0";
                break;
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
                t = "EVDO_A";
                break;
            case TelephonyManager.NETWORK_TYPE_1xRTT:
                t = "1xRTT";
                break;
            case TelephonyManager.NETWORK_TYPE_HSDPA:
                t = "HSDPA";
                break;
            case TelephonyManager.NETWORK_TYPE_HSUPA:
                t = "HSUPA";
                break;
            case TelephonyManager.NETWORK_TYPE_HSPA:
                t = "HSPA";
                break;
            case TelephonyManager.NETWORK_TYPE_IDEN:
                t = "IDEN";
                break;
            case TelephonyManager.NETWORK_TYPE_EVDO_B:
                t = "EVDO_B";
                break;
            case TelephonyManager.NETWORK_TYPE_LTE:
                t = "LTE";
                break;
            case TelephonyManager.NETWORK_TYPE_EHRPD:
                t = "eHRPD";
                break;
            case TelephonyManager.NETWORK_TYPE_HSPAP:
                t = "HSPA+";
                break;
            default:
                t = UNKNOWN;
                break;
        }

        return t;
    }

    /**
     * get mobile type in string
     *
     * @param code Integer phone type
     * @return string type
     * @see #getNetworkType(Context, int)
     */
    public static String getPhoneType(int code) {
        String t = UNKNOWN;
        switch (code) {
            case TelephonyManager.PHONE_TYPE_CDMA:
                t = "CDMA";
                break;
            case TelephonyManager.PHONE_TYPE_GSM:
                t = "GSM";
                break;
            case TelephonyManager.PHONE_TYPE_NONE:
                t = "NONE";
                break;
            case TelephonyManager.PHONE_TYPE_SIP:
                t = "SIP";
                break;
            default:
                t = "GSM";
                break;
        }

        return t;
    }

    /**
     * @param pState
     * @return
     * @see TelephonyManager#getSimState()
     */
    public static String getSimState(int pState) {
        String state = UNKNOWN;
        switch (pState) {
            case TelephonyManager.SIM_STATE_UNKNOWN:

                break;
            case TelephonyManager.SIM_STATE_ABSENT:
                state = "无 SIM卡";
                break;
            case TelephonyManager.SIM_STATE_PIN_REQUIRED:
                state = "PIN 锁定";
                break;
            case TelephonyManager.SIM_STATE_PUK_REQUIRED:
                state = "PUK 锁定";
                break;
            case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
                state = "网络锁定";
                break;
            case TelephonyManager.SIM_STATE_READY:
                state = "正常";
                break;
            default:
                state = "正常";
                break;
        }

        return state;
    }

    public static String getMIP() {
        if (!NetworkHelper.ip.contains(UNKNOWN)) {
            return NetworkHelper.ip;
        }
        new Thread() {

            @Override
            public void run() {
                try {
                    URL url = new URL("http://city.ip138.com/ip2city.asp");
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    int responseCode = connection.getResponseCode();
                    if (responseCode == 200) {
                        String str = inputStream2String(connection.getInputStream());
                        Pattern pattern = Pattern.compile("\\d+\\.\\d+\\.\\d+\\.\\d+]");
                        Matcher is = pattern.matcher(str);
                        if (is.find()) {
                            NetworkHelper.ip = is.group().replace("]", "");
                        } else {
                            NetworkHelper.ip = "0.0.0.0";
                        }
                    }
                } catch (Exception e) {
                    NetworkHelper.ip = "0.0.0.0";
                }
            }

            ;
        }.start();

        return NetworkHelper.ip;
    }

    /**
     * get str from inputstream
     *
     * @param stream
     * @return string
     * @throws IOException
     */
    public static String inputStream2String(InputStream stream) throws IOException {
        int count = 0;
        byte buffer[] = new byte[1024];
        StringBuilder builder = new StringBuilder();
        while ((count = stream.read(buffer)) != -1) {
            builder.append(new String(buffer, 0, count));
        }
        return builder.toString().trim();
    }

    public static String getYYS(int code) {
        String val = UNKNOWN;
        switch (code) {
            case 46000:
                val = "中国移动";
                break;
            case 46001:
                val = "中国联通";
                break;
            case 46002:
                val = "中国移动";
                break;
            case 46003:
                val = "中国电信";
                break;
            case 46007:
                val = "中国移动";
                break;
            case 46009:
                val = "中国电信";
                break;
            case 45412:
                val = "中國移動香港";
                break;
            case 45504:
                val = "澳門電訊";
                break;
            case 45501:
                val = "澳門電訊";
                break;
            case 46601:
                val = "遠傳電信";
                break;
            case 46688:
                val = "和信電訊";
                break;
            case 46689:
                val = "威寶電信";
                break;
            case 46690:
                val = "震旦電信";
                break;
            case 46692:
                val = "中華電信";
                break;
            case 46693:
                val = "東信電訊";
                break;
            case 46697:
                val = "台灣大哥大";
                break;
            case 46699:
                val = "泛亞電信";
                break;
            default:
                break;
        }

        return val;
    }

    public static boolean isAirModeOn(Context context) {
        if (Build.VERSION.SDK_INT >= 17) {
            return (Settings.System.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) == 1);
        } else {
            return (Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1);
        }
    }

}
